;(function($){
  var nameProvince = 'clsProvince';//省|市
  var nameCity = 'clsCity';//地州市
  var nameCounty = 'clsCounty';//区|县
  var nameTowns = 'clsTowns';//乡|镇|社区
  var nameVallage = 'clsVallage';//村|居委会
  var nameGroup = 'clsGroup';//组|街道
  
  var clsProvince = '.'+nameProvince;
  var clsCity = '.'+nameCity;
  var clsCounty = '.'+nameCounty;
  var clsTowns = '.'+nameTowns;
  var clsVallage = '.'+nameVallage;
  var clsGroup = '.'+nameGroup;

  var arrsProvince = [clsProvince,clsCity,clsCounty,clsTowns,clsVallage,clsGroup];
  var arrsCity = [clsCity,clsCounty,clsTowns,clsVallage,clsGroup];
  var arrsCounty = [clsCounty,clsTowns,clsVallage,clsGroup];
  var arrsTowns = [clsTowns,clsVallage,clsGroup];
  var arrsVallage = [clsVallage,clsGroup];
  var arrsGroup = [clsGroup];
  
  window.pulldownArea = {
    /**初始化下拉列表,推荐,pulldownArea.initOpts(urlQueryArea,data,'#divAreaOpts','clsProvince',"{\"clsProvince\":\"选择省市\",\"clsCity\":\"选择地州市\",\"clsCounty\":\"选择区|县\",\"clsTowns\":\"选择乡|镇|社区\",\"clsVallage\":\"选择村|居委会\",\"clsGroup\":\"选择组|街道\",\"cease\":\"clsCounty\"}");*/
    //todo 注意 用到哪一个级别就写哪些级别的nameXxx即可，此处的是全部级别其中的cease表示要终止请求的key,此处的值为clsCounty表示等于该值时不往后端请求数据，若不想限制级别的那cease可以不写，示例在 service.html
    initOpts : function(url,data,dom,cls,objs){
      var json = JSON.parse(objs);//转换为json
      objs = JSON.stringify(objs)//转换为字符串
      var showText = json[cls] || '选择选项';
      var parentDom = dom.substring(1,dom.length);
      var code200 = AppKey.code.code200 == data.code;
      showText = code200 ? showText : data.msg;
      $(dom +' .'+cls+' button span.spanText').text(showText);
      var html = '<li><a class="'+parentDom+'Label" href=\'javascript:pulldownArea.handerChange("'+url+'","","'+dom+'","'+cls+'",'+objs+');\'>'+showText+'</a></li>';
      if(code200){
        $.each(data.data,function(index,item){
          $('#' + item.value).remove();
          html += '<li><a class="'+parentDom+item.value+'" href=\'javascript:pulldownArea.handerChange("'+url+'","'+item.value+'","'+dom+'","'+cls+'",'+objs+');\' id="'+item.value+'">'+item.label+'</a></li>';
        });
      }
      $(dom +' .'+cls).css({"display":"inline"});
      $(dom +' .'+cls+' ul').html(html);
      $(dom +' .'+cls+' input').remove();//避免重复生成
      $(dom +' .'+cls + ' span button').append('<input type="hidden"/>');
      this.handerValue(dom,cls);//解决重新打开时的处理移除dom和置空
    },
    handerChange : function(url,value,dom,cls,objs){
      this.handerValue(dom,cls);
      var parentDom = dom.substring(1,dom.length);
      if(value){
        var showText = $(dom +' .'+cls+' li a.'+parentDom+value).text();
        $(dom +' .'+cls+' button span.spanText').text(showText);
        $(dom +' .'+cls+' input').val(value);
        var json = JSON.parse(objs);
        var cease = json['cease'] || '';//隐藏指定的下拉列表
        if(cease == cls){
          return;
        }
        layerFn.queryGetHintResult(url,{pid:value},function(data){
          var node = '';
          if(cls == nameProvince){
            node = nameCity;
          }else if(cls == nameCity){
            node = nameCounty;
          }else if(cls == nameCounty){
            node = nameTowns;
          }else if(cls == nameTowns){
            node = nameVallage;
          }else if(cls == nameVallage){
            node = nameGroup;
          }
          if(node != ''){
            pulldownArea.initOpts(url,data,dom,node,objs);
          }
        });
      }else{
        var showText = $(dom +' .'+cls+' li a.'+parentDom+'Label').text();
        $(dom +' .'+cls+' button span.spanText').text(showText);
        $(dom +' .'+cls+' input').val('');
      }
    },
    /*处理input值,值在弹窗时已重置*/
    handerValue : function(dom,cls){
      if(cls == nameProvince){
        this.handerHideValue(dom,arrsProvince,arrsCity);
      }else if(cls == nameCity){
        this.handerHideValue(dom,arrsCity,arrsCounty);
      }else if(cls == nameCounty){
        this.handerHideValue(dom,arrsCounty,arrsTowns);
      }else if(cls == nameTowns){
        this.handerHideValue(dom,arrsTowns,arrsVallage);
      }else if(cls == nameVallage){
        this.handerHideValue(dom,arrsVallage,arrsGroup);
      }
    },
    /*处理值和隐藏下一级*/
    handerHideValue : function(dom,arrsValue,arrsDisplay){
      $.each(arrsValue,function(index,item){
        $(dom +' '+item+' input').val('');//全部清空
      });
      $.each(arrsDisplay,function(index,item){
        $(dom +' '+item).css({"display":"none"});//除了当前的dom外其余的全部隐藏
        $(dom +' '+item +' ul li').remove();//移除下拉列表
      });
    },
    /*新建或编辑时重置:pulldownArea.handerReset('#divOptsList','clsProvince','选择类型选项');*/
    handerReset : function(dom,clsLevel,showText){
      this.handerValue(dom,clsLevel);//从哪1级的子节点开始隐藏,不带.
      $(dom+' .'+clsLevel+' .spanText').text(showText);
    },
    initSelectOpts: function(url,data,dom,cls,objs){
      var json = JSON.parse(objs);//转换为json
      var showText = json[cls] || '选择选项';
      var code200 = AppKey.code.code200 == data.code;
      showText = code200 ? showText : data.msg;
      var html = '<option value="" style="text-align: center">'+showText+'</option>';
      if(code200){
        $.each(data.data,function(index,item){
          html += '<option value="'+item.value+'">'+item.label+'</option>';
        });
      }
      $(dom +' .'+cls+' select').html(html);
      $(dom +' .'+cls+' select').change(function (res) {
        var selectedValue = $(this).val();
        pulldownArea.handerSelectChange(url,selectedValue,dom,cls,objs);
      });
      $(dom +' .'+cls).css({"display":"block"});
      this.handerSelectValue(dom,cls);
    },
    handerSelectChange : function(url,value,dom,cls,objs){
      this.handerSelectValue(dom,cls);
      if(value && value != '0'){
        layerFn.queryGetHintResult(url,{pid:value},function(data){
          var node = '';
          if(cls == nameProvince){
            node = nameCity;
          }else if(cls == nameCity){
            node = nameCounty;
          }else if(cls == nameCounty){
            node = nameTowns;
          }else if(cls == nameTowns){
            node = nameVallage;
          }else if(cls == nameVallage){
            node = nameGroup;
          }
          if(node != ''){
            pulldownArea.initSelectOpts(url,data,dom,node,objs);
          }
        });
      }
    },
    /*处理option值,值在弹窗时已重置*/
    handerSelectValue : function(dom,cls){
      if(cls == nameProvince){
        this.handerHideOption(dom,arrsProvince,arrsCity);
      }else if(cls == nameCity){
        this.handerHideOption(dom,arrsCity,arrsCounty);
      }else if(cls == nameCounty){
        this.handerHideOption(dom,arrsCounty,arrsTowns);
      }else if(cls == nameTowns){
        this.handerHideOption(dom,arrsTowns,arrsVallage);
      }else if(cls == nameVallage){
        this.handerHideOption(dom,arrsVallage,arrsGroup);
      }
    },
    /*处理值和隐藏下一级*/
    handerHideOption : function(dom,arrsValue,arrsDisplay){
      $.each(arrsDisplay,function(index,item){
        $(dom +' '+item).css({"display":"none"});//除了当前的dom外其余的全部隐藏
        $(dom +' '+item +' select option').remove();//移除下拉列表
      });
    },
    //回写下拉列表,若有特殊需求依据此来修改即可。用法: pulldownArea.showPullDown();如:if(map != null){pulldownArea.showPullDown('#div_store_area',map.data);}
    showPullDown : function(selector,map){
      function pullDownList(dom,cls,text){
        $(dom +' .'+cls).css("display","inline");
        $(dom +' .'+cls+' button span.spanText').text(text);
      }
      $.each(map,function(k,v){
        try{
          if(k == 'province_name'){//省
            pullDownList(selector,'clsProvince',v);
            $(selector +' .clsProvince').attr('title','重新选择即可修改');
            return true;
          }
          if(k == 'city_name'){//市
            pullDownList(selector,'clsCity',v);
            return true;
          }
          if(k == 'county_name'){//区|县
            pullDownList(selector,'clsCounty',v);
            return true;
          }
          if(k == 'towns_name'){//乡|镇|社区
            pullDownList(selector,'clsTowns',v);
            return true;
          }
          if(k == 'vallage_name'){//村|居委会
            pullDownList(selector,'clsVallage',v);
            return true;
          }
          if(k == 'group_name'){//组|街道
            pullDownList(selector,'clsGroup',v);
            return true;
          }
        }catch(e){}
      });
    },
    //编辑时回写,参考页面 areaProxy.html
    pulldownHtml : function(url,data,dom,cls,objs){
      objs = JSON.stringify(objs)//转换为字符串
      var html = '';
      var parentDom = dom.substring(1,dom.length);
      $.each(data,function(index,item){
        $('#' + item.value).remove();
        html += '<li><a class="'+parentDom+item.value+'" href=\'javascript:pulldownArea.handerChange("'+url+'","'+item.value+'","'+dom+'","'+cls+'",'+objs+');\' id="'+item.value+'">'+item.label+'</a></li>';
      });
      $(dom +' .'+cls+' input').remove();//避免重复生成
      return html;
    },
    fillLabelValue : function(dom,cls,value,url,jsonArea,list){
      if(url != null){//不适用于省级，因为省级已请求并填充下拉列表
        var html = pulldownArea.pulldownHtml(url,list,dom,cls,jsonArea);
        $(dom+' .'+cls+' ul').html(html);
        $(dom+' .'+cls).css({"display":"inline"});
        $(dom+' .'+cls+' span button').append('<input type="hidden"/>');
      }
      $(dom+' .'+cls+' input').val(value);//赋值
      winFn.editDropdown(dom+' .'+cls+' button span.spanText',value);//显示
    }
  };
})(jQuery);